package com.kusch;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * https://baomidou.com/pages/981406/#%E5%8F%AF%E9%80%89%E9%85%8D%E7%BD%AE
 *
 * @author Zhangzp
 * @date 2022年08月12日 11:11
 */
public class GoodcolQuickGenerator {
    public static void main(String[] args) {

//        String url = "jdbc:oracle:thin:@192.168.1.53:1521/orcl";
//        String username = "MUSES_V153_DEV";
//        String password = "MUSES_V153_DEV";

        String url = "jdbc:oracle:thin:@192.168.1.53:1521/orcl";
        String username = "jszh_fxq";
        String password = "jszh_fxq";

        //oracle 填这个
        String schema = "JSZH_FXQ";

        //你项目下的java文件夹全路径
        String javaPath = "/Users/kusch/Documents/myProjects/back/generate-code/src/main/java";
        //作者
        String author = "Mr.Anonymous";
        //你项目的 mapper.xml放置位置全路径
        String xmlPath = "/Users/kusch/Documents/myProjects/back/generate-code/src/main/resources/mapper/";
        //包名，一般都是这个
        String pg = "com.goodcol.muses";
        //模块名
        String moduleName = "zsk";
        //要生成的表名
        List<String> tables = new ArrayList<>();
        tables.add("FXQ_KBM_CASE");

        DataSourceConfig.Builder dbBuilder
                = new DataSourceConfig.Builder(url, username, password);
        if (StringUtils.isNotBlank(schema)) {
            dbBuilder.schema(schema);
        }
        //数据类型转换, 一般用不到这个
        //        dbBuilder.typeConvert(new OracleTypeConvert());
        FastAutoGenerator
                .create(
                        dbBuilder
                )
                .globalConfig(builder -> {
                    builder
                            .outputDir(javaPath)
                            .author(author)
                            //                .enableSwagger()
                            .dateType(DateType.ONLY_DATE)
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            //关闭生成完成后打开文件夹
                            .disableOpenDir();
                })
                .packageConfig(builder -> {
                    builder
                            .parent(pg)
                            //模块包名字
                            .moduleName(moduleName)
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            //.xml("mapper.xml")
                            .controller("controller")
                            .other("dto")
                            //这里定义xml输出到 /resources/mapper/xml/ares 文件夹下
                            .pathInfo(Collections.singletonMap(OutputFile.xml, xmlPath));


                })
                .templateConfig(builder -> {
                    builder
                            .disable()
                            .entity("/goodcol/entity.java")
                            .service("/goodcol/service.java")
                            .serviceImpl("/goodcol/serviceImpl.java")
                            .mapper("/goodcol/mapper.java")
                            .xml("/goodcol/mapper.xml")
                            .controller("/goodcol/controller.java");
                })
                .strategyConfig(builder -> {
                    builder
                            //开启跳过视图
                            .enableSkipView()
                            .disableSqlFilter()
                            //.likeTable(new LikeTable("SYS_ANNOUNCEMENT"))
                            .addInclude(tables)

                            .entityBuilder()
                            .enableLombok()
                            //.enableChainModel()
                            .enableTableFieldAnnotation()
                            .fileOverride()

                            .controllerBuilder()
                            .enableRestStyle()

                            .mapperBuilder();
                    //加上@Mapper注解
                    //.enableMapperAnnotation()
                })
                .injectionConfig(consumer -> {
                    Map<String, String> customFile = new HashMap<>();
                    // DTO.java 是后缀，，例如你的实体名叫 AAA   那么你生成的DTO就叫  AAADTO.java
                    customFile.put("QueryDTO.java", "/goodcol/entityDTOQuery.java.ftl");
                    customFile.put("AddDTO.java", "/goodcol/entityDTOAdd.java.ftl");
                    customFile.put("UpdateDTO.java", "/goodcol/entityDTOUpdate.java.ftl");
                    customFile.put("DeleteDTO.java", "/goodcol/entityDTODelete.java.ftl");

                    //VO
                    customFile.put("QueryVO.java", "/goodcol/entityVOQuery.java.ftl");
                    customFile.put("PageVO.java", "/goodcol/entityVOPage.java.ftl");

                    consumer.customFile(customFile);
                })
                .templateEngine(new EnhanceFreemarkerTemplateEngine())
                .execute();
    }
}
